package com.byxm.wlgw.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import com.byxm.wlgw.bean.GwcxxBean;
import com.byxm.wlgw.common.DataBaseAccess;
import com.byxm.wlgw.query.GwcxxQuery;

/**
 * 购物车信息
 * @author Administrator
 *
 */

public class GwcxxService {
	//配置init
    private static final String BUNDLE_NAME = "init";
	private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
	//图片服务器地址
	private String imgServiceUrl= RESOURCE_BUNDLE.getString("imgservice.server.url");
	
	
	GwcxxQuery gwcxxQuery=new GwcxxQuery();
	DataBaseAccess dba = null;	
	
	/**
	 * 按条件删除购物车信息
	 * @param gwcxx
	 * @throws Exception 
	 */
	public void deleteGwcxx(GwcxxBean gwcxx) throws Exception{
		try {
			dba=new DataBaseAccess();
			dba.executeUpdate(gwcxxQuery.delteGwcxx(gwcxx));
			dba.commit();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
	}
	/**
	 * 获取购物车信息
	 * @throws Exception 
	 */
	public List<GwcxxBean> getGwcxx(int yhxxid) throws Exception{
		List<GwcxxBean> list=new ArrayList<GwcxxBean>();
		try {
			dba=new DataBaseAccess();
			ResultSet rs=dba.executeQuery(gwcxxQuery.getGwcxxByYhxxid(yhxxid));
			while(rs.next()){
				GwcxxBean gwcxx=new GwcxxBean();
				gwcxx.setGwcxxid(rs.getInt("gwcxxid"));
				gwcxx.setYhxxid(rs.getInt("yhxxid"));
				gwcxx.setSpxxid(rs.getInt("spxxid"));
				gwcxx.setSpxhnr(rs.getString("spxhnr"));
				gwcxx.setSpxhid(rs.getInt("spxhid"));
				gwcxx.setSpsl(rs.getInt("spsl"));
				gwcxx.setSpsj(rs.getFloat("spsj"));
				gwcxx.setSpbt(rs.getString("spbt"));
				gwcxx.setSptp(imgServiceUrl+rs.getString("sptp"));
				list.add(gwcxx);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return list;
	}
	public List<GwcxxBean> getGwcxxNoUrl(int yhxxid) throws Exception{
		List<GwcxxBean> list=new ArrayList<GwcxxBean>();
		try {
			dba=new DataBaseAccess();
			ResultSet rs=dba.executeQuery(gwcxxQuery.getGwcxxByYhxxid(yhxxid));
			while(rs.next()){
				GwcxxBean gwcxx=new GwcxxBean();
				gwcxx.setGwcxxid(rs.getInt("gwcxxid"));
				gwcxx.setYhxxid(rs.getInt("yhxxid"));
				gwcxx.setSpxxid(rs.getInt("spxxid"));
				gwcxx.setSpxhnr(rs.getString("spxhnr"));
				gwcxx.setSpxhid(rs.getInt("spxhid"));
				gwcxx.setSpsl(rs.getInt("spsl"));
				gwcxx.setSpsj(rs.getFloat("spsj"));
				gwcxx.setSpbt(rs.getString("spbt"));
				gwcxx.setSptp(rs.getString("sptp"));
				list.add(gwcxx);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return list;
	}
	
	/**
	 * 添加商品进购物车
	 * @throws Exception 
	 */
	public int addSptogwc(GwcxxBean gwcxx) throws Exception{
		int flag=0;
		try {
			dba=new DataBaseAccess();
			PreparedStatement ps=dba.getPstmt(gwcxxQuery.addSptogwcSql());
			ps.setObject(1,gwcxx.getYhxxid());
			ps.setObject(2,gwcxx.getSpxxid());
			ps.setObject(3,gwcxx.getSpxhid());
			ps.setObject(4,gwcxx.getSpsl());
			flag=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return flag;
	}
	/**
	 * 看看有没有相同商品
	 * @param gwcxx
	 * @return
	 * @throws Exception 
	 */
	public boolean getGwcxxByxtsp(GwcxxBean gwcxx) throws Exception{
		boolean flag=false;
		try {
			dba=new DataBaseAccess();
			PreparedStatement ps=dba.getPstmt(gwcxxQuery.getGwcxxByxtsp());
			ps.setObject(1, gwcxx.getYhxxid());
			ps.setObject(2, gwcxx.getSpxxid());
			ps.setObject(3, gwcxx.getSpxhid());
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				flag=true;
			}
			ps.close();
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		
		return flag;
	}
	/**
	 * 更新购物车信息
	 * @param gwcxxid
	 * @param spsl
	 * @throws Exception 
	 */
	public void updateGwcxx(int gwcxxid,int spsl) throws Exception
	{
		try {
			dba=new DataBaseAccess();
			PreparedStatement ps=dba.getPstmt(gwcxxQuery.updateGwcxxSql());
			ps.setObject(1,spsl);
			ps.setObject(2, gwcxxid);
			int i=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		
		
		
	}
	/**
	 * 添加商品数量
	 * @param gwcxx
	 * @return
	 * @throws Exception
	 */
	public void addGwcxxSpsl(GwcxxBean gwcxx) throws Exception{
		try {
			dba=new DataBaseAccess();
			PreparedStatement ps=dba.getPstmt(gwcxxQuery.addGwcxxSpsl());
			ps.setObject(1,gwcxx.getSpsl());
			ps.setObject(2, gwcxx.getYhxxid());
			ps.setObject(3,gwcxx.getSpxxid());
			ps.setObject(4,gwcxx.getSpxhid());
			int i=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
	}
}
